﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceAgent.UserAccountService;
using System.ServiceModel;

namespace ServiceAgent
{
    public class UserAccountServiceAgent : IDisposable
    {
        UserAccountServiceClient proxy;

        public UserAccountServiceAgent()
        {
            proxy = new UserAccountServiceClient();
        }

        public UserDTO GetUserByID(int IDFilter)
        {
            try
            {
                proxy.Open();
                UserDTO obj =
                    proxy.GetUserByID(IDFilter);
                proxy.Close();
                return obj;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public UserDTO GetUserByTenTaiKhoan(string TenTaiKhoanFilter)
        {
            try
            {
                proxy.Open();
                UserDTO obj =
                    proxy.GetUserByTenTaiKhoan(TenTaiKhoanFilter);
                proxy.Close();
                return obj;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public UserDTO GetUserByUserNamePassword(string username, string password)
        {
            try
            {
                proxy.Open();
                UserDTO obj =
                    proxy.GetUserByUserNamePassword(username, password);
                proxy.Close();
                return obj;
            }
            catch (FaultException)
            {
                throw;
            }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void UpdateUserAccount(UserDTO objdto)
        {
            try
            {
                proxy.Open();
                proxy.UpdateUserAccount(objdto);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void ChangePassword(int id, string password)
        {
            try
            {
                proxy.Open();
                proxy.ChangePassword(id, password);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public bool Validate(string username, string password)
        {
            try
            {
                proxy.Open();
                var result = proxy.Validate(username, password);
                proxy.Close();
                return result;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        #region Disposable
        public void Dispose()
        {
            this.DisposeExistingServiceClientIfRequired();
        }

        private void DisposeExistingServiceClientIfRequired()
        {
            if (proxy != null)
            {
                try
                {
                    if (proxy.State == CommunicationState.Faulted)
                    {
                        proxy.Abort();
                    }
                    else
                    {
                        proxy.Close();
                    }
                }
                catch
                {
                    proxy.Abort();
                }

                proxy = null;
            }
        }
        #endregion
    }
}
